package week16;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.directory.SearchControls;
public class TestJDBC {
	static Connection conn;
	static Statement stmt;
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
	// 1. 注册数据库的驱动,8.0版本
	Class.forName("com.mysql.cj.jdbc.Driver");
	// 2.通过DriverManager获取数据库连接，连接的数据库名字superm
	String url = "jdbc:mysql://localhost:3306/superm?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
	String username = "root";//安装mysql默认的用户名
	String password = "123456";//安装mysql设置的密码
	conn = DriverManager.getConnection(url, username, password);
	// 3.通过Connection对象获取Statement对象
	 stmt = conn.createStatement();
	// 4.使用Statement执行SQL语句。
	String sql = "select * from admininfo";//查询的sql语句
	ResultSet rs = stmt.executeQuery(sql);
	// 5. 操作ResultSet结果集
	
	while (rs.next()) {
		 
		String name = rs.getString("name");//获取当前记录的name字段的值
		String psw = rs.getString("password");
		 
		System.out.println("name="+name+"/"+"password="+psw );
	}
	
	search( "john","000");
	// 6.回收数据库资源
	rs.close();	
	stmt.close();
	conn.close();
	}

	private static void search(String name, String password) throws SQLException {
		// TODO Auto-generated method stub
		// 4.使用Statement执行SQL语句。
		String sql = "SELECT * from admininfo where NAME=? AND PASSWORD=?";//查询的sql语句
		PreparedStatement ptst=conn.prepareStatement(sql);
		//给？传值，将sql语句补充完整
		ptst.setString(1, name);//1---第一个？
		ptst.setString(2, password);//2---第二个？
		//执行查询
		ResultSet rs=ptst.executeQuery();
		//结果集非空---找到
		//结果集空---没找到
		if(rs.next()) {
			System.out.println("匹配");
		}else {
			System.out.println("不匹配");
		}
	}
}
